(function() {
	var SceneManager = window.SceneManager = function() {
		// 1表示欢迎屏幕，2表示教程界面，3表示游戏内容，3表示GameOver
		this.sceneNumber = 1;
		// 场景管理器负责实例化东西
		game.bg = new Background();
		game.bird = new Bird();
		game.land = new Land();
		//		game.pipe = new Pipe();
		// logo的y值
		this.logoY = 48;
		// button_play的X,Y值
		this.button_playX = game.canvas.width / 2 - 58;
		this.button_playY = game.canvas.height;
		// 添加监听
		this.bindEvent();

	}
	// 更新
	SceneManager.prototype.updata = function() {
		switch(this.sceneNumber) {
			case 1:
				// 让logo移动
				this.logoY += 10;
				if(this.logoY >= 150) {
					this.logoY = 150;
				}
				// 让按钮移动
				this.button_playY -= 16;
				if(this.button_playY <= 330) {
					this.button_playY = 330
				}
				break;
			case 2:
				// 小鸟扑打翅膀
				game.bird.wing();
				// 改变教程图透明度
				if(this.tutorialOpacityIsDown) {
					this.tutorialOpacity -= 0.1;
				} else {
					this.tutorialOpacity += 0.1;
				}
				// 如果到头了，反过来
				if(this.tutorialOpacity < 0.1 || this.tutorialOpacity > 0.9) {
					this.tutorialOpacityIsDown = !this.tutorialOpacityIsDown;
				}

				break;
			case 3:
				// 小鸟更新
				game.bird.updata();
				// 背景更新
				game.bg.updata();
				// 大地更新
				game.land.updata();
				// 管子的实例化
				(game.fno % 150 == 0) && (new Pipe());
				// 渲染所有管子
				for(var i = 0; i < game.pipe_Arr.length; i++) {
					game.pipe_Arr[i] && game.pipe_Arr[i].updata();
				}
				break;
			case 4:
				if(game.bird.y > game.canvas.height * 0.78 - 17) {
					this.isBird = true;
				}

				this.birdfno++;
				// 鸟是否落地
				if(!this.isBird) {
					game.bird.y += 1.4 * this.birdfno;
				} else {
					// 爆炸序列
					game.fno % 4 == 0 && this.boomStep++;
					//						if(this.boomStep > 11) {
					//							this.boomStep = 11;
					//						}
				}
				// 白屏变回来
				this.maskOpacity -= 0.1;
				if(this.maskOpacity < 0) {
					this.maskOpacity = 0;
				}
				break;

		}
	}
	// 渲染
	SceneManager.prototype.render = function() {
		// 根据当前是第几个场景，，来决定做什么
		switch(this.sceneNumber) {
			case 1:
				// 渲染背景
				game.bg.render();
				// 渲染大地
				game.land.render();
				// 渲染小鸟
				game.bird.render();
				game.bird.x = game.canvas.width / 2;
				game.bird.y = 260;
				// logo图片
				game.ctx.drawImage(game.r["logo"], game.canvas.width / 2 - 89, this.logoY);
				// 按钮图片
				game.ctx.drawImage(game.r["button_play"], this.button_playX, this.button_playY);

				break;
			case 2:
				// 渲染背景
				game.bg.render();
				// 渲染大地
				game.land.render();
				// 渲染小鸟
				game.bird.render();
				// 教程图片
				game.ctx.save();
				game.ctx.globalAlpha = this.tutorialOpacity;
				game.ctx.drawImage(game.r["tutorial"], game.canvas.width / 2 - 57, 250);
				game.ctx.restore();
				break;
			case 3:

				// 渲染背景
				game.bg.render();
				// 渲染大地
				game.land.render();

				// 渲染管子
				for(var i = 0; i < game.pipe_Arr.length; i++) {
					game.pipe_Arr[i] && game.pipe_Arr[i].render();
				}
				// 打印当前分数
				var scoreLength = game.score.toString().length; // 得到当前分数的位数
				// 循环语句去设置图片的显示，有一个基准位置就是self.canvas.width / 2 - scoreLength / 2 * 34
				for(var i = 0; i < scoreLength; i++) {
					game.ctx.drawImage(game.r["shuzi" + game.score.toString().charAt(i)], game.canvas.width / 2 - scoreLength / 2 * 34 + 34 * i, 50);
				}
				// 渲染小鸟
				game.bird.render();
				break;
			case 4:
				// 渲染背景
				game.bg.render();
				// 渲染大地
				game.land.render();

				// 渲染管子
				for(var i = 0; i < game.pipe_Arr.length; i++) {
					game.pipe_Arr[i] && game.pipe_Arr[i].render();
				}

				// 渲染小鸟
				if(!this.isBird) {
					game.bird.render();
				} else {
					// 渲染爆炸特效
					if(this.boomStep <= 11) {
						game.ctx.drawImage(game.r["boom_" + this.boomStep], game.bird.x - 24 - 36, game.bird.y - 24 - 60)
					} else {
						this.enter(5);
					}

				}
				// 渲染大白屏
				game.ctx.fillStyle = "rgba(255,255,255," + this.maskOpacity + ")";
				game.ctx.fillRect(0, 0, game.canvas.width, game.canvas.height);
				// 打印当前分数
				var scoreLength = game.score.toString().length; // 得到当前分数的位数
				// 循环语句去	设置图片的显示，有一个基准位置就是self.canvas.width / 2 - scoreLength / 2 * 34
				for(var i = 0; i < scoreLength; i++) {
					game.ctx.drawImage(game.r["shuzi" + game.score.toString().charAt(i)], game.canvas.width / 2 - scoreLength / 2 * 34 + 34 * i, 50);
				}

				break;
			case 5:
				// 渲染背景
				game.bg.render();
				// 渲染大地
				game.land.render();

				// 渲染管子
				for(var i = 0; i < game.pipe_Arr.length; i++) {
					game.pipe_Arr[i] && game.pipe_Arr[i].render();
				}

				// 打印当前分数
				var scoreLength = game.score.toString().length; // 得到当前分数的位数
				// 循环语句去	设置图片的显示，有一个基准位置就是self.canvas.width / 2 - scoreLength / 2 * 34
				for(var i = 0; i < scoreLength; i++) {
					game.ctx.drawImage(game.r["shuzi" + game.score.toString().charAt(i)], game.canvas.width / 2 - scoreLength / 2 * 34 + 34 * i, 50);
				}
				// 渲染重新再来
				game.ctx.drawImage(game.r["text_game_over"], game.canvas.width / 2 - 102, 150);
				game.ctx.font = "24px cursive";
				game.ctx.fillStyle = "black";
				game.ctx.fillText("请点击一下，重新开始！",game.canvas.width / 2 -120,350)
				break;

		}
	}
	// 进入某个场景要做的事情
	SceneManager.prototype.enter = function(number) {
		this.sceneNumber = number;
		switch(this.sceneNumber) {
			case 1:
				// 进入1号场景瞬间要做的事情
				this.logoY = -48;
				this.button_playY = game.canvas.height;
				game.bird = new Bird();
				game.score = 0;
				break;
			case 2:
				game.bird.y = 180;
				// tutorial的透明度0~1
				this.tutorialOpacity = 1;
				this.tutorialOpacityIsDown = true;
				break;
			case 3:
				// 管子数组清空
				game.pipe_Arr = new Array();
				break;
			case 4:
				// 死亡动画,游戏界面白一下
				this.maskOpacity = 1;
				// 小鸟是否已经触底
				this.isBird = false;
				// 小帧编号
				this.birdfno = 0;
				// 特效图片
				this.boomStep = 0
				break;
			
		}
	}
	// 添加事件监听
	SceneManager.prototype.bindEvent = function() {
		var self = this;
//		game.canvas.onclick = function(event){
//			clickHandler(event.clientX,event.clientY);
//		}
		game.canvas.addEventListener("touchstart",function(event){
			event.preventDefault();
			var finger = event.touches[0];
			clickHandler(finger.clientX,finger.clientY);
		})
		function clickHandler(mousex,mousey) {
			

			// 点击的时候判断当前是第几个场景
			switch(self.sceneNumber) {
				case 1:
					if(mousex > self.button_playX && mousex < self.button_playX + 116 && mousey > self.button_playY && mousey < self.button_playY + 70) {
						// 说明用户点击到了按钮上
						self.enter(2); // 去到2号场景
					}
					break;
				case 2:
					// 去到3号场景
					self.enter(3);
					break;
				case 3:
					game.bird.fly();
					break;
				case 5:
					self.enter(1);
					break;
			}
		}
	}
})();